package com.example.administrator.daggerdemo.utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.telephony.TelephonyManager;


import com.example.administrator.daggerdemo.MyApplication;

import java.util.List;
import java.util.UUID;



/**
 * Created by Administrator on 2016/6/3.
 */
public class PhoneUtils {
    private static String imei = null;
    private  static String imsi=null;
    private  static String android_version=null;
    private  static String app_version=null;
    private  static String phone_model=null;
    private  static String device_token=null;

    /**
     * 获取手机IMEI
     * @return
     */
    public static String getIMEI() {
        if (imei == null) {
            TelephonyManager tm = (TelephonyManager
                    ) MyApplication.getInstance().getSystemService(Context.TELEPHONY_SERVICE);
            imei = tm.getDeviceId();
        }
        return imei;
    }

    public static boolean isApkDebugable(Context context,String packageName) {
        try {

            PackageInfo pkginfo = context.getPackageManager().getPackageInfo(
                    packageName, PackageManager.GET_ACTIVITIES);
            if (pkginfo != null ) {
                ApplicationInfo info= pkginfo.applicationInfo;
                return (info.flags&ApplicationInfo.FLAG_DEBUGGABLE)!=0;
            }

        } catch (Exception e) {

        }
        return false;
    }

    /**
     * 获取手机android 版本
     * @return
     */
    public static String getAndroidVersion() {
        if (android_version == null) {

            int ver= android.os.Build.VERSION.SDK_INT;
            android_version ="android"+ver;
        }
        return android_version;
    }

    /**
     * 获取手机IMSI
     * @return
     */
    public static String getIMSI() {
        if (imsi == null) {
            TelephonyManager tm = (TelephonyManager
                    ) MyApplication.getInstance().getSystemService(Context.TELEPHONY_SERVICE);
            imsi = tm.getSubscriberId();
        }
        return imsi;
    }

    /**
     * 获取手机型号
     * @return
     */
    public static String getPhoneModel() {
        if (phone_model == null) {
            phone_model = android.os.Build.MODEL;
        }
        return phone_model;
    }

    /**
     * 获取APP版本
     * @return
     */
    public static String getAPPVersion() {
        if (app_version == null) {
            PackageManager packageManager = MyApplication.getInstance().getPackageManager();
            // getPackageName()是你当前类的包名，0代表是获取版本信息
            PackageInfo packInfo = null;
            try {
                packInfo = packageManager.getPackageInfo(MyApplication.getInstance().getPackageName(),0);
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
                return null;
            }
            app_version = packInfo.versionName;
        }
        return app_version;
    }

    public static String getDeviceToken() {
       /* if (device_token == null) {
            device_token = JPushInterface.getRegistrationID(MyApplication.getInstance());
        }*/
        return "190e35f7e04cf3d4a8b";
    }


    public static String getApp_id(){
        String device_token =  XmlUtils.get("app_id",null);
        if (device_token == null)
        {
            device_token =  UUID.randomUUID().toString().replaceAll("-","");
            XmlUtils.put("app_id",device_token);
        }
        return device_token;
    }

    /**
     * 获取指定安装本地指定应用信息
     *
     * @param context
     * @param packageName：应用包名
     * @return
     */
    public static PackageInfo getPackInfo(Context context, String packageName) {
        PackageInfo result = null;
        //获取packagemanager
        final PackageManager packageManager = context.getPackageManager();
        //获取所有已安装程序的包信息
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        if (packageInfos != null) {
            for (PackageInfo packageInfo:packageInfos)
            {
                if (packageInfo.applicationInfo.packageName.equals(packageName))
                {
                    result = packageInfo;
                    break;
                }
            }
        }
        return result;
    }

    public static boolean checkPermission(Activity activity,String permission,int requestCode){
        boolean result = true;
        if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED)
        {
            ActivityCompat.requestPermissions(activity, new String[]{permission}, requestCode);
        }
        return result;
    }
}
